package cn.kgc.crowdfundingadmin03.controller;

import cn.kgc.crowdfundingadmin03.common.CrowdFundingConstant;
import cn.kgc.crowdfundingadmin03.common.ResultEntity;
import cn.kgc.crowdfundingadmin03.pojo.Admin;
import cn.kgc.crowdfundingadmin03.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-02-19 15:15
 */
@Controller
public class AdminController {


    @Resource
    AdminService adminService;

    @RequestMapping("/admin/get_by_id")
    @ResponseBody
    public Admin getByID(Integer id, Model model) {
        Admin admin = adminService.selectByPrimaryKey(id);
        return admin;
    }

    /*管理员登陆 */
 /*   @RequestMapping("/admin/do/login")
    public String doLogin(
            @RequestParam("loginAcct") String loginAcct,
            @RequestParam("userPswd") String userPswd,
            Model model,
            HttpSession session){
        Admin admin = adminService.login(loginAcct, userPswd);
        if (admin==null){
            model.addAttribute(CrowdFundingConstant.ATTR_NAME_MESSAGE, CrowdFundingConstant.MESSAGE_LOGIN_FAILED);
            return "admin-login";
        }
        session.setAttribute(CrowdFundingConstant.ATTR_NAME_LOGIN_ADMIN, admin);
        return "redirect:/admin/to/main/page.html";
    }*/

    /*异常处理功能*/
    @ExceptionHandler(value = Exception.class)
    public String catchException(Exception exception, Model model) {
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_ERROR_MESSAGE, exception);
        return "system-error";
    }

    /*退出功能*/
    @RequestMapping("/admin/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/index";
    }


    //分页查询用
    @RequestMapping("/admin/query/for/search")
    @PreAuthorize(value = "hasAnyRole('超级管理员','经理')")
    public String queryForSearch(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "keyword", defaultValue = "") String keyword,
            Model model) {
        PageInfo<Admin> pageInfo = adminService.queryForKeywordSearch(pageNum, pageSize, keyword);
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO, pageInfo);
        return "admin-page";
    }


    //批量删除
    @ResponseBody
    @RequestMapping("/admin/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> adminIdList) {
        adminService.batchRemove(adminIdList);
        return ResultEntity.successWithoutData();
    }

    //增加
    @RequestMapping("/admin/save")
    public String saveAdmin(Admin admin) {
        adminService.saveAdmin(admin);
        //删除成功跳转到查询页面  分页查询
        return "redirect:/admin/query/for/search";
    }


    //修改前的查询 根据id查询
    @RequestMapping("/admin/to/edit/page")
    public String toEditPage(@RequestParam("adminId") Integer adminId, Model model) {
        Admin admin = adminService.selectByPrimaryKey(adminId);
        model.addAttribute(CrowdFundingConstant.ATTR_ADMIN, admin);
        return "admin-edit";
    }

    @RequestMapping("/admin/update")
    public String updateAdmin(Admin admin, Integer pageNum) {
        adminService.updateAdmin(admin);
        //当前页 存储过去
        return "redirect:/admin/query/for/search?pageNum=" + pageNum;
    }
}
